from django.conf import settings
from django.contrib.auth.models import User
from django.contrib import admin
from django.db import models

# In the settings and such you will be able to change the screen name
# Here we are using App Grouping to describe a set of applications
# in a more commercial setting like a consulting company this might be used as a list of clients
class ApplicationGrouping(models.Model):
    Grouping_Name = models.CharField(max_length=64,help_text='Enter the name of the Application Grouping')
    Grouping_Description = models.TextField(help_text='Enter the description of the Application Grouping')
    AGAdded_Date = models.DateTimeField('date added',auto_now_add=True,editable=False)
    AGLast_Date = models.DateTimeField('last updated',auto_now=True,editable=False)
    AGAdded_User = models.CharField(max_length=64,editable=False)
    AGLast_User = models.CharField(max_length=64,editable=False)

    def __unicode__(self):
        return self.Grouping_Name

# Long term the concept of a sub group of a sub group needs to be established
# for now we will just setup for a single level of subgroups.
class SubGroup(models.Model):
    ParentGrouping = models.ForeignKey(ApplicationGrouping)
    SubGroupName = models.CharField('Subgroup Name:',max_length=200)
    SGAdded_Date = models.DateTimeField('date added',auto_now_add=True,editable=False)
    SGLast_Date = models.DateTimeField('last updated',auto_now=True,editable=False)
    SGAdded_User = models.CharField(max_length=64,editable=False)
    SGLast_User = models.CharField(max_length=64,editable=False)

    def __unicode__(self):
        return self.SubGroupName

# Application defines the information about the app that are not dynamicly updated
# For Instance, it contains the the app Names but not the servers they are on
class Application(models.Model):
    AGrouping = models.ForeignKey(ApplicationGrouping)
    App_Name = models.CharField('App. Name',max_length=200,unique=True)
    App_URI = models.CharField('App. URI',max_length=1024,help_text='Enter in only the part of the URL after the machine name')
    App_Version_File = models.CharField('Version File Name',max_length=256,blank=True)
    App_Description = models.TextField('App Description',help_text='Enter the Description of the application along with the App Owner and Development Lead')
    App_Listener_Port = models.SmallIntegerField(blank=True,null=True   )
    AAdded_Date = models.DateTimeField('date added',auto_now_add=True,editable=False)
    ALast_Date = models.DateTimeField('last updated',auto_now=True,editable=False)
    AAdded_User = models.CharField(max_length=64,editable=False)
    ALast_User = models.CharField(max_length=64,editable=False)
    def __unicode__(self):
        return self.App_URI

# Sub-Applications are additional Web Apps listed in the configurations
# The are linked to other apps for organization purposes  App also doesn't have a
# Listner Port or Version File because it should use the same as the primary app
class SubApplication(models.Model):
    Primary_App = models.ForeignKey(Application)
    App_Name = models.CharField('App. Name',max_length=200)
    App_URI = models.CharField('App. URI',max_length=1024,help_text='Enter in only the part of the URL after the machine name')
    App_Description = models.TextField('App Description',help_text='Enter the Description of the application along with the App Owner and Development Lead')
    AAdded_Date = models.DateTimeField('date added',auto_now_add=True,editable=False)
    ALast_Date = models.DateTimeField('last updated',auto_now=True,editable=False)
    AAdded_User = models.CharField(max_length=64,editable=False)
    ALast_User = models.CharField(max_length=64,editable=False)
    def __unicode__(self):
        return self.App_URI

# If a support group test page has been developed this represents the link to it with directions on how to use it
class ApplicationTestPage(models.Model):
    App_Name = models.ForeignKey(Application)
    TPName = models.CharField('Test Page Name',max_length=64,help_text='Name the test page.(useful if there are several tests possible.)')
    Test_Located = models.CharField('Where is the Test Located?',max_length=200,help_text='What is the URL/URI for the test(i.e. http://someserver/someapp/test or /someapp/test/ or /test/)')
    Test_Instruction = models.TextField('How do I use it?',help_text='Enter any steps needed to run the test on the test page.')
    TPAdded_Date = models.DateTimeField('date added',auto_now_add=True,editable=False)
    TPLast_Date = models.DateTimeField('last updated',auto_now=True,editable=False)
    TPAdded_User = models.CharField(max_length=64,editable=False)
    TPLast_User = models.CharField(max_length=64,editable=False)
    def __unicode__(self):
        return self.TPName

# This table will be used by a batch process to gather this information
class ApplicationVersion(models.Model):
    VName = models.ForeignKey(Application)
    Version_info = models.CharField(max_length=128)
    AVAdded_Date = models.DateTimeField('date added',auto_now_add=True,editable=False)
    AVLast_Date = models.DateTimeField('last updated',auto_now=True,editable=False)
    AVAdded_User = models.CharField(max_length=64,editable=False)
    AVLast_User = models.CharField(max_length=64,editable=False)

    def __unicode__(self):
        return self.Version_info

# Server Models represent the type of Software involved.  For Example: WAS, WAS Portal, Weblogic, Tomcat
class ServerModel(models.Model):
    SMName = models.CharField('Application Server Type',max_length=128,help_text='The Common name of the aplication server software. i.e. WebSphere, Tomcat, Weblogic')
    SMVersion_Number = models.CharField('Software Version',max_length=64,help_text='Enter the version of the App Server Software.  i.e. 6.0.2.21, 5.x,4.0.x')
    SMAdded_Date = models.DateTimeField('date added',auto_now_add=True,editable=False)
    SMLast_Date = models.DateTimeField('last updated',auto_now=True,editable=False)
    SMAdded_User = models.CharField(max_length=64,editable=False)
    SMLast_User = models.CharField(max_length=64,editable=False)

    def __unicode__(self):
        type_name= self.SMName+' '+self.SMVersion_Number
        return type_name

# This gives us a way to add Admin servers like WAS Deployment Managers and supports
# using automated batch building discovery.
class ServerName(models.Model):
    SVersion_Number = models.ForeignKey(ServerModel)
    FQDN = models.CharField(max_length=128)
    IP_Address = models.IPAddressField()
    SGrouping = models.ForeignKey(ApplicationGrouping)
    SAdded_Date = models.DateTimeField('date added',auto_now_add=True,editable=False)
    SLast_Date = models.DateTimeField('last updated',auto_now=True,editable=False)
    SAdded_User = models.CharField(max_length=64,editable=False)
    SLast_User = models.CharField(max_length=64,editable=False)
    TYPES_OF_SERVERS = (
                        ('D', 'Deployment Manager'),
                        ('N', 'Node'),
                        ('S', 'Stand Alone')
                         )
    Server_Class = models.CharField(max_length=1, choices=TYPES_OF_SERVERS)
    TEIR_OF_SERVER = (
                        ('D', 'Development'),
                        ('I', 'Integration'),
                        ('Q', 'QA/Test'),
                        ('S', 'Staging'),
                        ('P', 'Production')
                         )
    Server_Teir = models.CharField(max_length=1, choices=TEIR_OF_SERVER)
    def __unicode__(self):
        return self.FQDN

class ApplicationToServer(models.Model):
    AppURI = models.ForeignKey(Application)
    ServerName = models.ForeignKey(ServerName)
    Listener_Port = models.SmallIntegerField(blank=True)
    MAdded_Date = models.DateTimeField('date added',auto_now_add=True,editable=False)
    MLast_Date = models.DateTimeField('last updated',auto_now=True,editable=False)
    MAdded_User = models.CharField(max_length=64,editable=False)
    MLast_User = models.CharField(max_length=64,editable=False)
    def __unicode__(self):
        return self.AppURI
